package com.wuchengdong.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wuchengdong.web.extend.utils.R;
import com.wuchengdong.web.pojo.Appointment;
import com.wuchengdong.web.pojo.Order;
import com.wuchengdong.web.pojo.Payment;
import com.wuchengdong.web.pojo.dto.AppointmentDTO;
import com.wuchengdong.web.service.AppointmentService;
import com.wuchengdong.web.service.OrderService;
import com.wuchengdong.web.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/*
 * 业务流：
 *   用户提出预约 -> 后台生成： 预约表数据 订单表数据 支付表数据
 *   初始状态： 预约pending 订单pending 支付pending   -> 用户支付
 *            预约confirm 订单paid    支付succeed   （此时相当于支付完事了）
 *            -> 用户到店  预约completed 订单completed
 *            -> 用户未到店 预约canceled 订单canceled -> 删除支付记录 退款
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    //-------------------用户端-------------------------------------------------

    /**
     * 思路 可能要用DTO去接，创建预约记录 订单 (要检查支付状态)
     */
    @PostMapping("/userClient")
    public R<String> createNewAppointment(@RequestBody AppointmentDTO appointmentDTO) {
        Order order = appointmentService.createNewAppointment(appointmentDTO);
        return R.success(order.getId().toString());
    }

    //-------------------商家端-------------------------------------------------
    @GetMapping
    public R<List<Appointment>> getAll() {
        return R.success(appointmentService.list());
    }

    @GetMapping("/new")
    public R<List<Appointment>> getNewAppointment() {
        List<Appointment> list = appointmentService.list(
                new LambdaQueryWrapper<Appointment>()
                        .eq(Appointment::getStatus, "init")
                        .or()
                        .eq(Appointment::getStatus, "confirmed")
        );
        return R.success(list);
    }

    @PutMapping("/nextStep")
    public R<String> updateStatus(@RequestBody Appointment appointment) {
        if (appointmentService.updateStatus(appointment)) {
            return R.success("success");
        }else {
            return R.error("error");
        }
    }

}
